package com.doule.farmerassistant;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;

import com.doule.farmerassistant.autoupdate.UpdateManager;
import com.doule.farmerassistant.plan.PlanFragment;
import com.doule.farmerassistant.task.TaskFragment;

/**
 * Main fragment activity.
 * 
 * @author riguang
 * 
 */
public class MainActivity extends FragmentActivity {
    /**
     * Class tag.
     */
    private static final String TAG = "MainActivity";
    private UpdateManager mUpdateManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentTabHost mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        Bundle b = new Bundle();
        // TextView v = new TextView(this);
        b.putString("key", "Simple");
        // v = new TextView(this);
        // v.setText(getString(R.string.task));
        // v.setTextSize(20);
        // v.setGravity(Gravity.CENTER);
        // v.setBackgroundResource(R.drawable.green_long);
        // mTabHost.addTab(mTabHost.newTabSpec(getString(R.string.task))
        // .setIndicator(v), TaskFragment.class, b);
        mTabHost.addTab(mTabHost.newTabSpec(getString(R.string.task))
                .setIndicator(getString(R.string.task)), TaskFragment.class, b);
        //
        b = new Bundle();
        b.putString("key", "Contacts");
        // v = new TextView(this);
        // v.setText(getString(R.string.plan));
        // v.setTextSize(20);
        // v.setGravity(Gravity.CENTER);
        // v.setBackgroundResource(R.drawable.green_long);
        // mTabHost.addTab(mTabHost.newTabSpec(getString(R.string.plan))
        // .setIndicator(v), PlanFragment.class, b);

        mTabHost.addTab(mTabHost.newTabSpec(getString(R.string.plan))
                .setIndicator(getString(R.string.plan)), PlanFragment.class, b);

        mUpdateManager = new UpdateManager(this, "http://192.168.1.100:4001/apk/latest");
        mUpdateManager.startCheck();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            // do something...
            Intent home = new Intent(Intent.ACTION_MAIN);
            home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            home.addCategory(Intent.CATEGORY_HOME);
            startActivity(home);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public final boolean onOptionsItemSelected(final MenuItem item) {
        int mid = item.getItemId();
        switch (mid) {
        case R.id.action_quit:
            finish();
            break;
        default:
            break;
        }

        return false;
    }

    @Override
    protected void onDestroy() {
        if (mUpdateManager != null) {
            mUpdateManager.discard();
        }

        super.onDestroy();
    }
}
